package com.masterclient.app.modules.chezhubang;

import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.masterclient.app.BaseActivity;
import com.masterclient.app.R;
import com.masterclient.app.modules.bridge.BridgeActivity;
import com.masterclient.app.widget.TitlebarView;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @auther hjian
 * @date 2019/11/12.
 */
public class CzbAuthorizeActivity extends BaseActivity<CzbAuthorizePresenter> implements CzbAuthorizeContract.View {

    @BindView(R.id.czb_authorize)
    TextView czbAuthorize;
    @BindView(R.id.czb_protocol_ll)
    LinearLayout czbProtocolLl;
    @BindView(R.id.czb_titlebar)
    TitlebarView czbTitlebar;
    @BindView(R.id.czb_protocol_img)
    ImageView czbProtocolImg;
    @BindView(R.id.czb_protocol_text)
    TextView czbProtocolText;

    private boolean isSelect;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_czb_authorize);
        ButterKnife.bind(this);

        isSelect = true;
        SpannableString spanableInfo = new SpannableString(czbProtocolText.getText().toString().trim());
        spanableInfo.setSpan(new ClickableSpan() {
            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(0xFF7399E5);
                ds.setFlags(Paint.ANTI_ALIAS_FLAG);
            }

            @Override
            public void onClick(@NonNull View widget) {
                String procotol = getBundleString("czb_procotol");
                if (TextUtils.isEmpty(procotol))return;
                startActivitry(BridgeActivity.class,new String[][]{{"url",procotol},{"title","用户授权协议"}});
            }
        }, 38, 46, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        czbProtocolText.setText(spanableInfo);
        czbProtocolText.setHighlightColor(getResources().getColor(android.R.color.transparent));
        czbProtocolText.setMovementMethod(LinkMovementMethod.getInstance());

        czbTitlebar.setOnViewClick(new TitlebarView.onViewClick() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {

            }
        });
    }

    @Override
    public void authSuccess(Map<String,String> data) {
        showToast("授权成功");
        setResult(RESULT_OK);
        startActivitry(CzbWebActivity.class , new String[][]{{"url",data.get("sh_car_owner_url")}});
        finish();
    }

    @Override
    public void authFailed(String msg) {
        showToast(msg);
    }


    @OnClick({R.id.czb_authorize, R.id.czb_protocol_img, R.id.czb_protocol_text})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.czb_authorize:
                if (isSelect){
                    mPresenter.czbAuth();
                }else {
                    showToast("请先同意用户授权协议");
                }
                break;
            case R.id.czb_protocol_img:
//            case R.id.czb_protocol_text:
                isSelect = !isSelect;
                czbProtocolImg.setImageResource(isSelect?R.mipmap.danxuan_order_select:R.mipmap.danxuan_order);
                break;
        }
    }
}
